package top.chmin.project.cloud.sys.column.info;
import lombok.Getter;
import lombok.Setter;
import lombok.experimental.Accessors;
import top.chmin.project.cloud.common.*;
import top.chmin.project.cloud.tool.LocalTool;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;











/**
 * chmin
 * 2024-01-25 14:22:04
 * cloud-code
 */
@Getter
@Setter
@Accessors(chain = true)
public class ColumnInfo extends Model {
    /*###########auto#start###########*/
    // column编号
    private String columnCode;
    // column注释
    private String columnComment;
    // 关联取值范围字典
    private String columnDict;
    // 对应label字段
    private String columnLabel;
    // 字段长度
    private Integer columnLength;
    // column名称
    private String columnName;
    // 是否可Null
    private String columnNullable;
    // 数字位数
    private Integer columnPrecision;
    // 对应依赖字段
    private String columnRelate;
    // 小数精度
    private Integer columnScale;
    // 序列编码
    private String columnSequence;
    // column类型
    private String columnType;
    // table编号
    private String tableCode;
    /*###########auto##end############*/
    /* append $modelFieldAppender$ */

    public ColumnInfoModelCondition forInsert() {
        return new ColumnInfoModelCondition().forInsert();
    }

    public ColumnInfoModelCondition forUpdate() {
        return new ColumnInfoModelCondition().forUpdate();
    }

    public ColumnInfoModelCondition forDelete() {
        return new ColumnInfoModelCondition().forDelete();
    }

    public ColumnInfoModelCondition forSelect() {
        return new ColumnInfoModelCondition().forSelect();
    }

    public class ColumnInfoModelCondition extends Condition {
        ColumnInfoModelCondition parent = null;

        private ColumnInfoModelCondition() {
        }

        private ColumnInfoModelCondition(ColumnInfoModelCondition condition) {
            this.parent = condition;
        }

        private ColumnInfoModelCondition forInsert() {
            ColumnInfoModelCondition condition = new ColumnInfoModelCondition();
            List<String> columnNameList = new ArrayList<>();
            List<String> valueList = new ArrayList<>();
            /*###########auto#start###########*/
            columnNameList.add("column_code");
            valueList.add("?");
            condition.getValueList().add(getColumnCode());

            columnNameList.add("column_comment");
            valueList.add("?");
            condition.getValueList().add(getColumnComment());

            columnNameList.add("column_dict");
            valueList.add("?");
            condition.getValueList().add(getColumnDict());

            columnNameList.add("column_label");
            valueList.add("?");
            condition.getValueList().add(getColumnLabel());

            columnNameList.add("column_length");
            valueList.add("?");
            condition.getValueList().add(getColumnLength());

            columnNameList.add("column_name");
            valueList.add("?");
            condition.getValueList().add(getColumnName());

            columnNameList.add("column_nullable");
            valueList.add("?");
            condition.getValueList().add(getColumnNullable());

            columnNameList.add("column_precision");
            valueList.add("?");
            condition.getValueList().add(getColumnPrecision());

            columnNameList.add("column_relate");
            valueList.add("?");
            condition.getValueList().add(getColumnRelate());

            columnNameList.add("column_scale");
            valueList.add("?");
            condition.getValueList().add(getColumnScale());

            columnNameList.add("column_sequence");
            valueList.add("?");
            condition.getValueList().add(getColumnSequence());

            columnNameList.add("column_type");
            valueList.add("?");
            condition.getValueList().add(getColumnType());

            columnNameList.add("table_code");
            valueList.add("?");
            condition.getValueList().add(getTableCode());

            /*###########auto##end############*/
            /* append $modelForInsertFieldAppender$ */
            condition.getValueList().add(LocalTool.getUserId());
            condition.getValueList().add(LocalTool.getUserId());
            condition.getValueList().add(LocalDateTime.now());
            condition.getValueList().add(LocalDateTime.now());
            condition.setSql("INSERT INTO sys_column_info(%s, create_by, update_by, create_time, update_time) VALUES (%s, ?, ?, ?, ?)".formatted(
                    String.join(", ", columnNameList),
                    String.join(", ", valueList)
            ));
            return condition;
        }

        private ColumnInfoModelCondition forUpdate() {
            ColumnInfoModelCondition condition = new ColumnInfoModelCondition();
            /*###########auto#start###########*/
            if (columnCode != null) {
                condition.getUpdateList().add("column_code = ?");
                condition.getUpdateValueList().add(columnCode);
            }

            if (columnComment != null) {
                condition.getUpdateList().add("column_comment = ?");
                condition.getUpdateValueList().add(columnComment);
            }

            if (columnDict != null) {
                condition.getUpdateList().add("column_dict = ?");
                condition.getUpdateValueList().add(columnDict);
            }

            if (columnLabel != null) {
                condition.getUpdateList().add("column_label = ?");
                condition.getUpdateValueList().add(columnLabel);
            }

            if (columnLength != null) {
                condition.getUpdateList().add("column_length = ?");
                condition.getUpdateValueList().add(columnLength);
            }

            if (columnName != null) {
                condition.getUpdateList().add("column_name = ?");
                condition.getUpdateValueList().add(columnName);
            }

            if (columnNullable != null) {
                condition.getUpdateList().add("column_nullable = ?");
                condition.getUpdateValueList().add(columnNullable);
            }

            if (columnPrecision != null) {
                condition.getUpdateList().add("column_precision = ?");
                condition.getUpdateValueList().add(columnPrecision);
            }

            if (columnRelate != null) {
                condition.getUpdateList().add("column_relate = ?");
                condition.getUpdateValueList().add(columnRelate);
            }

            if (columnScale != null) {
                condition.getUpdateList().add("column_scale = ?");
                condition.getUpdateValueList().add(columnScale);
            }

            if (columnSequence != null) {
                condition.getUpdateList().add("column_sequence = ?");
                condition.getUpdateValueList().add(columnSequence);
            }

            if (columnType != null) {
                condition.getUpdateList().add("column_type = ?");
                condition.getUpdateValueList().add(columnType);
            }

            if (tableCode != null) {
                condition.getUpdateList().add("table_code = ?");
                condition.getUpdateValueList().add(tableCode);
            }

            /*###########auto##end############*/
            /* append $modelForUpdateFieldAppender$ */
            if (getUpdateBy() != null) {
                condition.getUpdateList().add("update_by = ?");
                condition.getValueList().add(LocalTool.getUserId());
            }
            if (getUpdateTime() != null) {
                condition.getUpdateList().add("update_time = ?");
                condition.getValueList().add(LocalDateTime.now());
            }
            condition.setSql("UPDATE sys_column_info#update##where##order##limit#");
            return condition;
        }

        private ColumnInfoModelCondition forDelete() {
            ColumnInfoModelCondition condition = new ColumnInfoModelCondition();
            condition.setSql("DELETE FROM sys_column_info#where##group##order##limit#");
            return condition;
        }

        private ColumnInfoModelCondition forSelect() {
            ColumnInfoModelCondition condition = new ColumnInfoModelCondition();
            condition.modelClass = ColumnInfo.class;
            condition.setSql("SELECT #select# FROM sys_column_info#where##group##order##limit#");
            return condition;
        }

        public ColumnInfoModelCondition distinct() {
            this.distinct = true;
            return this;
        }

        public ColumnInfoModelCondition limit(int limit) {
            this.limit = " LIMIT " + limit;
            return this;
        }

        public ColumnInfoModelCondition sub() {
            return new ColumnInfoModelCondition(this);
        }

        public ColumnInfoModelCondition finish() {
            if (!whereList.isEmpty()) {
                parent.whereList.add("(" + String.join(" OR ", whereList) + ")");
            }
            parent.whereValueList.addAll(whereValueList);
            return parent;
        }

        public ColumnInfoCondition toFlex() {
            ColumnInfoCondition flex = new ColumnInfoCondition();
            flex.setSql(sql);
            flex.setDistinct(distinct);
            flex.setModelClass(modelClass);
            flex.setSelectList(selectList);
            flex.setUpdateList(updateList);
            flex.setUpdateValueList(updateValueList);
            flex.setWhereList(whereList);
            flex.setWhereValueList(whereValueList);
            flex.setGroupList(groupList);
            flex.setOrderList(orderList);
            flex.setLimit(limit);
            return flex;
        }

        /*###########auto#start###########*/
        public ColumnCode columnCode() {
            return new ColumnCode();
        }

        public class ColumnCode extends StringModelColumn<ColumnInfoModelCondition> {
            private ColumnCode() {
                super(ColumnInfoModelCondition.this, "column_code", ColumnInfo.this.getColumnCode());
            }
        }

        public ColumnComment columnComment() {
            return new ColumnComment();
        }

        public class ColumnComment extends StringModelColumn<ColumnInfoModelCondition> {
            private ColumnComment() {
                super(ColumnInfoModelCondition.this, "column_comment", ColumnInfo.this.getColumnComment());
            }
        }

        public ColumnDict columnDict() {
            return new ColumnDict();
        }

        public class ColumnDict extends StringModelColumn<ColumnInfoModelCondition> {
            private ColumnDict() {
                super(ColumnInfoModelCondition.this, "column_dict", ColumnInfo.this.getColumnDict());
            }
        }

        public ColumnLabel columnLabel() {
            return new ColumnLabel();
        }

        public class ColumnLabel extends StringModelColumn<ColumnInfoModelCondition> {
            private ColumnLabel() {
                super(ColumnInfoModelCondition.this, "column_label", ColumnInfo.this.getColumnLabel());
            }
        }

        public ColumnLength columnLength() {
            return new ColumnLength();
        }

        public class ColumnLength extends IntegerModelColumn<ColumnInfoModelCondition> {
            private ColumnLength() {
                super(ColumnInfoModelCondition.this, "column_length", ColumnInfo.this.getColumnLength());
            }
        }

        public ColumnName columnName() {
            return new ColumnName();
        }

        public class ColumnName extends StringModelColumn<ColumnInfoModelCondition> {
            private ColumnName() {
                super(ColumnInfoModelCondition.this, "column_name", ColumnInfo.this.getColumnName());
            }
        }

        public ColumnNullable columnNullable() {
            return new ColumnNullable();
        }

        public class ColumnNullable extends StringModelColumn<ColumnInfoModelCondition> {
            private ColumnNullable() {
                super(ColumnInfoModelCondition.this, "column_nullable", ColumnInfo.this.getColumnNullable());
            }
        }

        public ColumnPrecision columnPrecision() {
            return new ColumnPrecision();
        }

        public class ColumnPrecision extends IntegerModelColumn<ColumnInfoModelCondition> {
            private ColumnPrecision() {
                super(ColumnInfoModelCondition.this, "column_precision", ColumnInfo.this.getColumnPrecision());
            }
        }

        public ColumnRelate columnRelate() {
            return new ColumnRelate();
        }

        public class ColumnRelate extends StringModelColumn<ColumnInfoModelCondition> {
            private ColumnRelate() {
                super(ColumnInfoModelCondition.this, "column_relate", ColumnInfo.this.getColumnRelate());
            }
        }

        public ColumnScale columnScale() {
            return new ColumnScale();
        }

        public class ColumnScale extends IntegerModelColumn<ColumnInfoModelCondition> {
            private ColumnScale() {
                super(ColumnInfoModelCondition.this, "column_scale", ColumnInfo.this.getColumnScale());
            }
        }

        public ColumnSequence columnSequence() {
            return new ColumnSequence();
        }

        public class ColumnSequence extends StringModelColumn<ColumnInfoModelCondition> {
            private ColumnSequence() {
                super(ColumnInfoModelCondition.this, "column_sequence", ColumnInfo.this.getColumnSequence());
            }
        }

        public ColumnType columnType() {
            return new ColumnType();
        }

        public class ColumnType extends StringModelColumn<ColumnInfoModelCondition> {
            private ColumnType() {
                super(ColumnInfoModelCondition.this, "column_type", ColumnInfo.this.getColumnType());
            }
        }

        public CreateBy createBy() {
            return new CreateBy();
        }

        public class CreateBy extends StringModelColumn<ColumnInfoModelCondition> {
            private CreateBy() {
                super(ColumnInfoModelCondition.this, "create_by", ColumnInfo.this.getCreateBy());
            }
        }

        public CreateTime createTime() {
            return new CreateTime();
        }

        public class CreateTime extends LocalDateTimeModelColumn<ColumnInfoModelCondition> {
            private CreateTime() {
                super(ColumnInfoModelCondition.this, "create_time", ColumnInfo.this.getCreateTime());
            }
        }

        public Id id() {
            return new Id();
        }

        public class Id extends IntegerModelColumn<ColumnInfoModelCondition> {
            private Id() {
                super(ColumnInfoModelCondition.this, "id", ColumnInfo.this.getId());
            }
        }

        public TableCode tableCode() {
            return new TableCode();
        }

        public class TableCode extends StringModelColumn<ColumnInfoModelCondition> {
            private TableCode() {
                super(ColumnInfoModelCondition.this, "table_code", ColumnInfo.this.getTableCode());
            }
        }

        public UpdateBy updateBy() {
            return new UpdateBy();
        }

        public class UpdateBy extends StringModelColumn<ColumnInfoModelCondition> {
            private UpdateBy() {
                super(ColumnInfoModelCondition.this, "update_by", ColumnInfo.this.getUpdateBy());
            }
        }

        public UpdateTime updateTime() {
            return new UpdateTime();
        }

        public class UpdateTime extends LocalDateTimeModelColumn<ColumnInfoModelCondition> {
            private UpdateTime() {
                super(ColumnInfoModelCondition.this, "update_time", ColumnInfo.this.getUpdateTime());
            }
        }

        /*###########auto##end############*/
        /* append $modelConditionFieldAppender$ */
    }
}
